package cn.rdq.iknessplatform.service.impl;

import cn.rdq.api.po.Catelog;
import cn.rdq.api.po.CatelogCollect;
import cn.rdq.api.vo.CatelogCollectedVo;
import cn.rdq.common.constant.CatelogDefault;
import cn.rdq.dao.CatelogCollectMapper;
import cn.rdq.iknessplatform.service.CatelogCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CatelogCollectServiceImpl extends ServiceImpl<CatelogCollectMapper, CatelogCollect> implements CatelogCollectService {

    @Resource
    private CatelogCollectMapper catelogCollectMapper;

    @Override
    public CatelogCollect getCollected(CatelogCollectedVo catelogCollectedVo) {
        QueryWrapper<CatelogCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", catelogCollectedVo.getUserId())
                .eq("catelog_id", catelogCollectedVo.getCatelogId());
        return getOne(wrapper);
    }

    @Override
    public List<Catelog> listInfoByUserId(CatelogCollectedVo catelogCollectedVo) {
        PageHelper.startPage(catelogCollectedVo.getCurrentPage(), catelogCollectedVo.getPageSize());
        return catelogCollectMapper.catelogInfoByUserId(catelogCollectedVo.getUserId());
    }

    @Override
    public boolean collect(CatelogCollectedVo catelogCollectedVo) {
        CatelogCollect collected = getCollected(catelogCollectedVo);
        if (ObjectUtils.isEmpty(collected)) {
            CatelogCollect catelogCollect = new CatelogCollect();
            catelogCollect.setCatelogId(catelogCollectedVo.getCatelogId());
            catelogCollect.setUserId(catelogCollectedVo.getUserId());
            return save(catelogCollect);
        }
        return false;
    }

    @Override
    public boolean cancelCollect(CatelogCollectedVo catelogCollectedVo) {
        QueryWrapper<CatelogCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id", catelogCollectedVo.getCatelogId())
                .eq("user_id", catelogCollectedVo.getUserId());
        return remove(wrapper);
    }

}
